﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using Microsoft.SqlServer.Types;
using System.IO;
using System.Reflection;
using System.Text;
using System.Drawing;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ABQBiking"].ConnectionString);
        SqlCommand comm = new SqlCommand("select top 5 geom from biketrails", conn);

        conn.Open();
        SqlDataReader reader = comm.ExecuteReader(CommandBehavior.CloseConnection);

        StringBuilder polygons = new StringBuilder();

        polygons.Append("function addPolygons(map){ ");

        int i = 0;

        Random r = new Random();

        while (reader.Read())
        {
            polygons.Append(createJavaScriptPolygon(reader["geom"] as SqlGeometry, "#ff0000", ++i));
        }

        polygons.Append(markers.ToString());

        polygons.Append("}");

        reader.Close();

        ScriptManager.RegisterClientScriptBlock(Header, Header.GetType(), "test", polygons.ToString(), true);
        //LINESTRING (1545347.0578094609 1529256.8540594578, 1545498.6957261264 1529174.915309459, 1545773.1390594579 1529063.0269761235, 1545937.8053094596 1529018.6936427951, 1546094.0265594609 1528999.6936427951, 1546256.5815594606 1528999.6936427951, 1546427.5807261244 1529014.4715594575, 1546566.9132261239 1529041.9157261252, 1546921.5782261267 1529101.0265594572, 1547120.0219761245 1529113.6932261288, 1547212.91030946 1529113.6932261288, 1547421.9094761275 1529109.4711427912, 1547677.352809459 1529079.9157261252, 1547899.0182261244 1529052.4711427912, 1548156.5728094578 1529027.1382261291, 1548321.2386427932 1529027.1382261291, 1548507.0157261267 1529046.1378094554, 1548652.6815594584 1529069.359892793, 1548800.458642792 1529088.359892793, 1548956.6803094596 1529107.359892793, 1549051.6798927933 1529107.359892793, 1549243.790309459 1529096.804476127, 1549372.5673927926 1529073.5823927894, 1549790.5653094575 1528965.9157261252, 1550419.6736427918 1528803.3611427918, 1550742.6719761267 1528731.5836427957, 1550932.6711427942 1528697.8061427921, 1551097.3369761258 1528674.584059462, 1551230.3365594596 1528657.6953094602, 1551380.2244761251 1528645.028642796, 1551521.6682261266 1528634.4732261226, 1551753.8898927942 1528628.1398927942, 1551899.5557261258 1528628.1398927942, 1552678.5519761257 1528642.9173927903, 1552828.4403094575 1528645.028642796, 1553843.8803094588 1528666.1394761279, 1554187.989892792 1528670.3615594581, 1554447.6553094573 1528676.6948927939, 1554557.4323927909 1528683.0282261223, 1554686.2094761245 1528697.8061427921, 1554722.0982261263 1528706.2503094599, 1554905.7640594579 1528744.2503094599, 1555064.0969761275 1528788.5832261294, 1555241.429476127 1528843.4719761238, 1555395.5398927927 1528873.0273927897, 1555558.0944761261 1528892.0273927897, 1555735.4269761257 1528894.1386427954, 1555946.5369761251 1528877.2494761273, 1556168.2032261267 1528841.3611427918, 1556592.5344761275 1528759.027809456, 1557715.6403094605 1528541.5844761282, 1558481.9703094587 1528395.9186427891, 1558690.9690594599 1528357.9186427891, 1558929.5236427933 1528315.6969761252, 1559174.4115594588 1528281.9190594554, 1559387.6328094602 1528265.0303094611, 1559583.9653094597 1528254.4748927951, 1559833.0753094591 1528248.1415594593, 1560077.9628094584 1528254.4748927951, 1560335.5173927918 1528275.585726127, 1560392.5169761255 1528281.9190594554, 1560658.5157261267 1528313.585726127, 1560840.0707261264 1528349.4744761288, 1561809.0661427937 1528569.0290594623, 1562197.5086427927 1528664.028642796, 1562260.8419761248 1528680.9173927903, 1562450.8411427923 1528706.2503094599, 1562683.0619761273 1528712.5836427957, 1562875.172392793 1528699.9169761241, 1563054.6161427908 1528666.1394761279, 1563286.8373927921 1528596.4732261226, 1563478.9473927915 1528514.1403094605, 1563664.7244761251 1528402.2519761249, 1563884.2786427923 1528252.3636427894, 1564003.3294761255 1528124.4894761294, 1564276.4944761246 1527779.1678094566, 1564642.4323927909 1527325.6107261255, 1564900.1353094578 1526985.4432261288, 1565137.2215594575 1526758.6644761264, 1565369.1544761248 1526598.8890594617, 1565688.7057261244 1526439.1132261232, 1566075.2598927915 1526274.1832261235, 1566302.0386427939 1526145.3319761232, 1566492.7386427932 1525959.7857261226, 1566745.2873927914 1525614.4640594572, 1566879.2928094603 1525140.2911427915, 1566874.1386427917 1524686.7340594605)
        //new SPCToDecimalDegrees(1545347.0578094609, 0.000000, 1529256.8540594578, 1640416.666667, 106.250000, 31.000000, 0.999900);        
    }

    private string createJavaScriptPolygon(SqlGeometry geom, string color, int count)
    {
        string polygon = geom.STBuffer(50).STAsText().ToSqlString().ToString();

        IEnumerable<string> pieces = polygon.Split(',', '(', ')').Select<string, string>(s => s.TrimStart()).Where<string>(s => s.Length > 0 && char.IsNumber(s[0]));

        StringBuilder googlePoly = new StringBuilder();

        googlePoly.AppendFormat("var polyline{0} = new GPolyline([", count.ToString());

        foreach (string p in pieces)
        {
            string[] eastingsAndNorthings = p.Split(' ');
            CentralNewMexico coords = new CentralNewMexico(Convert.ToDouble(eastingsAndNorthings[0]), Convert.ToDouble(eastingsAndNorthings[1]));
            googlePoly.AppendFormat("\r\n new GLatLng({0}, -{1}),", coords.Latitude.ToString(), coords.Longitude.ToString());
            markers.AppendFormat("\r\n map.addOverlay(new GMarker(new GLatLng({0}, -{1})));", coords.Latitude.ToString(), coords.Longitude.ToString());
        }

        googlePoly.Remove(googlePoly.Length-1, 1);

        googlePoly.AppendFormat("], \"{0}\", 1); \r\n map.addOverlay(polyline{1});\r\n \r\n \r\n ", color, count.ToString());

        return googlePoly.ToString();
    }

    private StringBuilder markers = new StringBuilder();
}

